package com.fanh.controller;

import com.fanh.common.api.CommonPage;
import com.fanh.common.api.CommonResult;
import com.fanh.dto.GuideCommentResult;
import com.fanh.dto.GuideLessonResult;
import com.fanh.dto.LessonCommentParam;
import com.fanh.model.*;
import com.fanh.service.GuideCourseService;
import com.fanh.service.GuidelessonService;
import com.fanh.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Controller
@Api(tags = "CourseController", description = "课程")
@RequestMapping("/a/guide")
public class CourseController {

    @Autowired
    private GuideCourseService guideCourseService;
    @Autowired
    private UserService userService;
    @Autowired
    private GuidelessonService guidelessonService;

    @ApiOperation("课程内容")
    @RequestMapping(value = "/t/lesson/content", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult content(@ApiParam(value = "lessonId", required = true)@RequestParam("lessonId")String lessonId) {
        GuideContent content =  guideCourseService.getGuideContentById(lessonId);
        return CommonResult.success(content);
    }

    @ApiOperation("文章批注接口")
    @RequestMapping(value = "/t/comment/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<GuideCommentResult>> commentList(@ApiParam(value = "lessonId", required = true)@RequestParam("lessonId")String lessonId) {
        List<GuideCommentResult> list =  guideCourseService.getGuideCommentBycontentId(lessonId);
        return CommonResult.success(list);
    }


    @ApiOperation("查询课程列表")
    @RequestMapping(value = "/t/course/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult courseList(@RequestParam(required = false, defaultValue = "1") Integer pageNo,
                           @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
         SysUser user = userService.getCurrentSysUser();
         if(user == null || user.getId() == null){
            return  CommonResult.failed("未登录！");
         }
      CommonPage<GuideCourse> list = guideCourseService.getCourseList(user.getId(),pageNo,pageSize);
         return CommonResult.success(list);
    }

    @ApiOperation("根据课程ID查询章节列表")
    @RequestMapping(value = "/t/lesson/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult lessonList(@ApiParam(value = "courseId", required = true)@RequestParam("courseId")String courseId,
                           @RequestParam(required = false, defaultValue = "1") Integer pageNo,
                           @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
       CommonPage<GuideLessonResult> page =  guidelessonService.getGuideLessonList(courseId,pageNo,pageSize);
        return CommonResult.success(page);
    }

    @ApiOperation("课程文件接口")
    @RequestMapping(value = "/t/comment/uploadFile", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult uploadFile(@ApiParam( required = true)@RequestParam(required = true)MultipartFile commentFile, @RequestBody GuideCommentFile courseCommentFile
                           ) {
        String url =  guideCourseService.fileUpload(commentFile,courseCommentFile);
        return CommonResult.success(url);

    }



    @ApiOperation("课程上传接口")
    @RequestMapping(value = "/t/comment/upload", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult upload(@RequestBody LessonCommentParam lessonCommentParam) {
      int count=  guideCourseService.uploadComment(lessonCommentParam);
      if(count >0){
          return CommonResult.success(count);
      }
        return CommonResult.failed("课程上传失败！");
    }


}
